/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sockets;

/**
 *
 * @author gracu
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.Thread.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class HiloSer extends Thread {

    private ServerSocket ss;
    boolean sirviendo = false;
    Thread hilo;

    public HiloSer() {
        try {

            ss = new ServerSocket(9999);
            sirviendo = true;
            System.out.println("ss: " + ss.toString() + "*****");
        } catch (IOException ex) {
            Logger.getLogger(HiloSer.class.getName()).log(Level.SEVERE, null, ex);
        }
        hilo = new Thread(this);
        hilo.start();
    }
    
    JLabel labelos = new JLabel();
    int daato = 0;
    Socket skCliente;

    public void parar() {
        sirviendo = false;
    }
    //public void generar(){daato=randomcasillas();}
    //public void stop(){dato=false;}

    @Override
    public void run() {
        try {
            skCliente = ss.accept();
            while (sirviendo) {

                System.out.println("-----ser");
                InputStream auxe = skCliente.getInputStream();
                DataInputStream flujos = new DataInputStream(auxe);
                String lectura = flujos.readUTF();

                if (lectura != null && lectura.length() > 0 && lectura.startsWith("punteo")) {
                    //--- galaga.setpunteoenemigo(lectura.substring(6));
                }
                System.out.println(lectura);
                //skCliente.close();
                try {
                    Thread.sleep(70);
                } catch (InterruptedException e) {
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(HiloSer.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("terminociclo");
        hilo = new Thread(this);
        hilo.start();
    }

    public void sendmensaje(String enviar) {
        OutputStream aux;
        try {
            aux = skCliente.getOutputStream();
            DataOutputStream flujo = new DataOutputStream(aux);
            flujo.writeUTF(enviar);
        } catch (IOException ex) {
            Logger.getLogger(HiloSer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
